<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap-theme.min.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/themes/icon.css"/>

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
	var rolename = null;
	$(function(){	
		$(".btn").click(function(){
			rolename = $(this).text();
			$("#showFuncs").tree({
				url:'${pageContext.request.contextPath}/showAllFuncsAction?rolename='+rolename,
				animate:true,
				lines:true,
				//显示checkbox
				checkbox:true,
				//关闭层级选中
				cascadeCheck:false,
				onLoadSuccess:function(node,data){
					$("#showFuncs").tree('expandAll');
				}
			});
			if(rolename=="所有"){
				$("#deleteButton").show();
				$("#modifyButton").hide();
			}else{
				$("#deleteButton").hide();
				$("#modifyButton").show();
			}
		});
		
		$("#deleteButton").click(function(){
			var nodes = $('#showFuncs').tree('getChecked');
	        var s = '';
	        for(var i=0; i<nodes.length; i++){
	            if (s != '') s += ',';
	            s += nodes[i].text;
	        }
	        $.ajax({
	        	type: 'post',
	        	url: '${pageContext.request.contextPath}/deleteFuncAction',
	        	data: {"funcs":s},
	        	dateType: 'text',
	        	success: function(data){
	        		if(data=="1"){
	        			$.messager.alert('提示','删除成功',"info",function(){
	        				$("#showFuncs").tree({
		        				url:'${pageContext.request.contextPath}/showAllFuncsAction?rolename='+rolename,
		        				animate:true,
		        				lines:true,
		        				//显示checkbox
		        				checkbox:true,
		        				//关闭层级选中
		        				cascadeCheck:false,
		        				onLoadSuccess:function(node,data){
		        					$("#showFuncs").tree('expandAll');
		        				}
		        			});
						});
	        		}else{
	        			$.messager.alert('提示','删除失败',"error");
	        		}
	        	}
	        });
		});
		
		$("#modifyButton").click(function(){
			 var nodes = $('#showFuncs').tree('getChecked');
	        var s = '';
	        for(var i=0; i<nodes.length; i++){
	            if (s != '') s += ',';
	            s += nodes[i].text;
	        }
	        $.ajax({
	        	type: 'post',
	        	url: '${pageContext.request.contextPath}/modifyFuncAction',
	        	data: {"funcs":s,"rolename":rolename},
	        	dataType: 'text',
	        	success:function(data){
	        		if(data=="1"){
	        			$.messager.show({
							title:'提示',
							msg:'修改成功',
							timeout:2000,
							showType:'slide'
						});
	        		}else{
	        			$.messager.alert('提示','修改失败',"error");	
	        		}
	        	}
	        });
		});
	});
</script>
<body class="container">
	<div class="btn-group">
	<button type="button" class="btn btn-default">所有</button>
	  <button type="button" class="btn btn-default">商家</button>
	  <button type="button" class="btn btn-default">客户</button>
	</div>
	<div style="padding:5px">
		<ul id="showFuncs"></ul>
	</div>
	<button id="modifyButton" style="display: none">修改</button>
	<button id="deleteButton" style="display: none">删除</button>
</body>
</html>